using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Umbraco.Ecommerce.BusinessLogic
{
	public class ProductVariant 
	{
		private string _ProductId;
		private string _Name;
		private decimal _price;
		private int _Id;
		private int _Order;
		private Product p;
		public ProductVariant(int id) 
		{_Id = id;}

		public Product Product {
			get {
				if (p == null) 
				{
					int ProductId = int.Parse(SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,"Select NodeId from ecommerceProductVariants where id = " + _Id).ToString());
					p = new Product(ProductId);
				}
				return p;
			}
		}

		public int Id {
			get {return _Id;}
		}
	

		public string ProductId 
		{
			get	{
				return SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,"select ProductId from ecommerceProductVariants where id = " +_Id).ToString();
			}	
			set {
				_ProductId = value;
				if (value.Trim() != ProductId && isUniqueProcuctId(value)) {
					SqlParameter[] SqlParams = new SqlParameter[] {	  new SqlParameter("@ProductId", value.Trim()),
																	  new SqlParameter("@Id", _Id)};
					SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN,CommandType.Text,"Update ecommerceProductVariants set ProductId = @ProductId  where id = @Id",SqlParams);
				}
			}
		}

		
		public string Name {
			get	{
				return SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,"select Name from ecommerceProductVariants where id = " +_Id).ToString();
			}	
			set {
				_Name = value;
				if (value.Trim() != Name) {
					SqlParameter[] SqlParams = new SqlParameter[] {  new SqlParameter("@Name", value.Trim()),
																	  new SqlParameter("@Id", _Id)};
					SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN,CommandType.Text,"Update ecommerceProductVariants set Name = @Name  where id = @Id",SqlParams);
				}
			}
		}

		public string FullName {
			get 
			{
				if (!this.Product.UseMultipleProductVariants)
					return this.Product.Name;
				return this.Product.Name + " ("+Name+")";
			}
		}
		
		
		public decimal Price 
			   {
			get	{
				return decimal.Parse(SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,"select Price from ecommerceProductVariants where id = " +_Id).ToString(),Store.NumberFormatInfo);
			}	
			set {
				_price = value;
				if (value != Price) {
					SqlParameter[] SqlParams = new SqlParameter[] {new SqlParameter("@Price", value),
																	  new SqlParameter("@Id", _Id)};
					SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN,CommandType.Text,"Update ecommerceProductVariants set Price = @Price where id = @Id",SqlParams);
				}
			}
		}

		public int Order {
			get	{
				return int.Parse(SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,"select [order] from ecommerceProductVariants where id = " +_Id).ToString());
			}	
			set {
				_Order = value;
				if (value != Order) {
					SqlParameter[] SqlParams = new SqlParameter[] {   new SqlParameter("@Order", value),
																	  new SqlParameter("@Id", _Id) };
					SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN,CommandType.Text,"Update ecommerceProductVariants set [Order] = @Order where id = @Id",SqlParams);
				}
			}
		}

		public static ProductVariant ProductVariantFromProductId(string ProductId) 
		{
			SqlParameter[] SqlParams = new SqlParameter[] {new SqlParameter("@productId",ProductId)};
			int ProductVariantId = int.Parse(SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,"select id from ecommerceProductVariants where productId = @productId",SqlParams).ToString());
			return new ProductVariant(ProductVariantId);
		}

		public static bool isUniqueProcuctId(string NewProductId) 
		{
			return (int.Parse(SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,"select count(id) as tmp from ecommerceProductVariants where ProductId = '" + NewProductId +"'").ToString()) == 0);
		}
	}
}